package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.text.Layout
import android.transition.Transition
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.view.setPadding
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import client.comm.baoding.R
import client.comm.baoding.api.bean.CateGoods
import client.comm.baoding.api.bean.GoodsIndex.Banner
import client.comm.baoding.databinding.LayoutHomeTjhwBannerBinding
import client.comm.baoding.databinding.LayoutHomeTjhwBinding
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.baoding.ui.GoodsGylDetailActivity
import client.comm.baoding.ui.WebActivity
import client.comm.baoding.utils.TextTool
import client.comm.commlib.base.BasePagingAdapter
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.base.SyBannerAdapter
import client.comm.commlib.comm_ui.ImageGalleryActivity
import client.comm.commlib.comm_ui.bean.CommImg
import client.comm.commlib.dipToPx
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.google.gson.Gson
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnBannerListener

/**
 * 精选商品
 *
 */
class HomeTjGoodAdapter(val context: Context, val manager: RequestManager) :
    BasePagingAdapter<CateGoods.Goods>(object : DiffUtil.ItemCallback<CateGoods.Goods>() {
        val g = Gson()

        override fun areItemsTheSame(
            oldItem: CateGoods.Goods,
            newItem: CateGoods.Goods
        ): Boolean {
            return true
        }

        override fun areContentsTheSame(
            oldItem: CateGoods.Goods,
            newItem: CateGoods.Goods
        ): Boolean {
            return g.toJson(oldItem).equals(g.toJson(newItem))
        }
    }) {
    var banner_list: List<Banner>? = null

    //设置商品label标签
    fun itemClick(bean: CateGoods.Goods) {
        if (bean.sku_status == 1) {
            context.startActivity(Intent(context, GoodsGylDetailActivity::class.java).apply {
                putExtra("goodsId", "${bean.goods_id}")
            })
        } else
            context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
                putExtra("goodsId", "${bean.goods_id}")
            })
    }

    fun itemClick2(bean: CateGoods.Goods) {
        if (bean.sku_status == 1) {
            context.startActivity(Intent(context, GoodsGylDetailActivity::class.java).apply {
                putExtra("goodsId", "${bean.goods_id}")
                putExtra("open", true)
            })
        } else
            context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
                putExtra("goodsId", "${bean.goods_id}")
                putExtra("open", true)
            })
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagingViewHoder {

        when (viewType) {
            99 -> {
                return PagingViewHoder(
                    DataBindingUtil.inflate<ViewDataBinding?>(
                        LayoutInflater.from(parent.context),
                        R.layout.layout_home_tjhw_banner,
                        parent,
                        false
                    ).apply {
                        (this as LayoutHomeTjhwBannerBinding).banner.indicator =
                            CircleIndicator(context)
                    }
                )
            }
        }

        return PagingViewHoder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.layout_home_tjhw,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: PagingViewHoder, position: Int) {

        when (holder.binding) {
            is LayoutHomeTjhwBannerBinding -> {

                val binding = holder.binding as LayoutHomeTjhwBannerBinding
                banner_list?.let {

                    binding.banner.setAdapter(object : MallBannerAdapter<Banner>(it) {
                        override fun onBindView(
                            holder: BannerViewHolder?,
                            data: Banner?,
                            position: Int,
                            size: Int
                        ) {

                            holder!!.imageView.setOnClickListener {

//                                if (!data?.image_url.isNullOrEmpty()) {
//                                    ImageGalleryActivity.show(
//                                        context, data?.image_url, true, false
//                                    )
//                                } else

                                if (!data?.link_url.isNullOrEmpty()) {
                                    if (!data?.link_url.isNullOrEmpty()) {

                                        if (data?.link_url!!.endsWith(".jpg")) {
                                            ImageGalleryActivity.show(
                                                context, data?.image_url, true, false
                                            )
                                        } else
                                            context.startActivity(Intent(
                                                context, WebActivity::class.java
                                            ).apply {
                                                putExtra("adInfo", data?.link_url)
                                                putExtra("title", data?.name)
                                            })
                                    }
                                }
                            }

                            val height = binding.banner.layoutParams.height
                            if (height == 0) {

                                manager.asBitmap().load(data?.image_url)
                                    .apply(RequestOptions().override(500, 500))
                                    .into(object : CustomTarget<Bitmap>() {

                                        override fun onResourceReady(
                                            resource: Bitmap,
                                            transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?
                                        ) {
                                            val width = resource.width
                                            val height = resource.height
                                            // 在这里可以使用width和height进行后续操作，比如打印或者存储等
                                            println("图片宽度: $width, 图片高度: $height")

                                            val params = binding.banner.layoutParams
                                            params.height =
                                                binding.banner.measuredWidth * height / width
                                            binding.banner.layoutParams = params
                                            holder!!.imageView.setImageBitmap(resource)
                                        }

                                        override fun onLoadCleared(placeholder: Drawable?) {
                                        }
                                    })
                            } else manager.load(data?.image_url).into(
                                holder!!.imageView
                            )


                        }
                    })

                    binding.banner.start()
                }

            }

            is LayoutHomeTjhwBinding -> {
                val binding = holder.binding as LayoutHomeTjhwBinding
                val bean = getItem(position)
                binding.bean = bean as CateGoods.Goods
                binding.event = this

                manager.load(bean.goods_img)
                    .into(binding.img)

                TextTool.getBuilder("¥").setAlign(Layout.Alignment.ALIGN_CENTER).setBold()
                    .setTextSize(14)
                    .append(bean.price.toString()).setForegroundColor(Color.parseColor("#E81837"))
                    .setBold()
                    .setTextSize(16)
                    .into(binding.tvPrice)
            }
        }
    }

    override fun bindItem(holder: PagingViewHoder, t: Any) {}

    override fun getItemLayout(): Int {
        return 0
    }

    override fun getItemViewType(position: Int): Int {
        val item = getItem(position)
        if (item?.name.equals("112233")) {
            return 99
        }
        return 88
    }

}


